var app = new Vue({
  el: "#app",
  data: {
    filterForm: {
      title: "",
      push_names: "",
      push_channel: "2",
      push_result: "",
      time: [],
      msg_type: "1",
      accept_user: "",
      accept_user_type: "accept_user_name",
      is_viewed: "",
    },
    infoTable: {
      list: [],
      total: 0,
      pageNumber: 1,
      pageSize: 10,
      loading: false,
    },
  },
  mounted() {
    this.getInfoList("init");
  },
  methods: {
    // 获取列表
    getInfoList(type) {
      if (type !== "init") {
        this.infoTable.pageNumber = 1;
      }
      this.infoTable.loading = true;
      apiGetInfoList({
        pageNumber: this.infoTable.pageNumber,
        pageSize: this.infoTable.pageSize,
        ...this.formatFilterForm(),
      })
        .then((res) => {
          const { data, total } = res;
          this.infoTable.list = data;
          this.infoTable.total = total;
          this.infoTable.loading = false;
          this.$refs.infoTable.doLayout();
          if (this.filterForm.msg_type === "2") {
            this.getAuctionDetail();
          }
        })
        .catch((err) => {
          this.infoTable.list = [];
          this.infoTable.total = 0;
          this.infoTable.loading = false;
          this.$message.error(err && err.msg);
        });
    },
    // 招标拍卖的分类和地区异步获取
    getAuctionDetail() {
      this.infoTable.list.forEach((item) => {
        item.cate = "获取中...";
        item.zone_names = "获取中...";
      });
      apiGetAuctionDetail({
        number_ids: this.infoTable.list.map((item) => item.number_id),
      }).then((res) => {
        const { data } = res;
        this.infoTable.list.forEach((item) => {
          const detail = data.find((i) => i.number_id === item.number_id);
          if (detail) {
            item.cate = detail.categoryname;
            item.zone_names = detail.address;
          } else {
            item.cate = "";
            item.zone_names = "";
          }
        });
      });
    },
    // 格式化筛选表单
    formatFilterForm() {
      const form = {};
      const list = ["accept_user", "accept_user_type", "time"];

      for (const key in this.filterForm) {
        if (!list.includes(key)) {
          form[key] = this.filterForm[key];
        }
      }
      let acceptTypeList = [
        "accept_user_name",
        "accept_user_company",
        "accept_user_phone",
      ];
      acceptTypeList.forEach((item) => {
        if (this.filterForm.accept_user_type === item) {
          form[item] = this.filterForm.accept_user;
        } else {
          form[item] = "";
        }
      });
      if (this.filterForm.time && this.filterForm.time.length > 0) {
        form.start_time = this.filterForm.time[0];
        form.end_time = this.filterForm.time[1];
      } else {
        form.start_time = "";
        form.end_time = "";
      }
      return form;
    },
    // 重置筛选表单
    resetfilterForm() {
      this.$refs.filterFormRef.resetFields();
      this.filterForm = {
        title: "",
        push_names: "",
        push_channel: "2",
        push_result: "",
        time: [],
        msg_type: "1",
        accept_user: "",
        accept_user_type: "accept_user_name",
        is_viewed: "",
      };
      this.infoTable.pageNumber = 1;
      this.getInfoList();
    },
    // 每页数
    sizeChangeHandle(table, val) {
      this.infoTable.pageSize = val;
      this.infoTable.pageNumber = 1;
      this.getInfoList("init");
    },
    // 当前页
    currentChangeHandle(table, val) {
      this.infoTable.pageNumber = val;
      this.getInfoList("init");
    },
  },
});
